
<!doctype html>
<html lang="zh" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
      
      <link rel="icon" href="../../assets/images/favicon.png">
      <meta name="generator" content="mkdocs-1.2.3, mkdocs-material-7.3.6">
    
    
      
        <title>录制并生成测试用例 - HttpRunner V3.x中文文档</title>
      
    
    
      <link rel="stylesheet" href="../../assets/stylesheets/main.a57b2b03.min.css">
      
        
        <link rel="stylesheet" href="../../assets/stylesheets/palette.3f5d1f46.min.css">
        
      
    
    
    
      
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700%7CRoboto+Mono&display=fallback">
        <style>:root{--md-text-font-family:"Roboto";--md-code-font-family:"Roboto Mono"}</style>
      
    
    
    
    
      


    
    
  </head>
  
  
    
    
    
    
    
    <body dir="ltr" data-md-color-scheme="" data-md-color-primary="none" data-md-color-accent="none">
  
    
    <script>function __prefix(e){return new URL("../..",location).pathname+"."+e}function __get(e,t=localStorage){return JSON.parse(t.getItem(__prefix(e)))}</script>
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#_1" class="md-skip">
          跳转至
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
      

<header class="md-header" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="Header">
    <a href="../.." title="HttpRunner V3.x中文文档" class="md-header__button md-logo" aria-label="HttpRunner V3.x中文文档" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54z"/></svg>

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            HttpRunner V3.x中文文档
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              录制并生成测试用例
            
          </span>
        </div>
      </div>
    </div>
    
    
    
      <label class="md-header__button md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5z"/></svg>
      </label>
      
<div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" aria-label="搜索" placeholder="搜索" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
      <label class="md-search__icon md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5z"/></svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12z"/></svg>
      </label>
      <nav class="md-search__options" aria-label="Search">
        
        <button type="reset" class="md-search__icon md-icon" aria-label="Clear" tabindex="-1">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z"/></svg>
        </button>
      </nav>
      
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="search-result">
          <div class="md-search-result__meta">
            正在初始化搜索引擎
          </div>
          <ol class="md-search-result__list"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
    
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    


<nav class="md-nav md-nav--primary" aria-label="Navigation" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="../.." title="HttpRunner V3.x中文文档" class="md-nav__button md-logo" aria-label="HttpRunner V3.x中文文档" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54z"/></svg>

    </a>
    HttpRunner V3.x中文文档
  </label>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../.." class="md-nav__link">
        名词解释
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../10-HttpRunner%E7%AE%80%E4%BB%8B/" class="md-nav__link">
        简介
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../20-HttpRunner%E5%AE%89%E8%A3%85/" class="md-nav__link">
        安装
      </a>
    </li>
  

    
      
      
      

  
  
    
  
  
    
    <li class="md-nav__item md-nav__item--active md-nav__item--nested">
      
      
        <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_4" type="checkbox" id="__nav_4" checked>
      
      
      
      
        <label class="md-nav__link" for="__nav_4">
          用户指南
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" aria-label="用户指南" data-md-level="1">
        <label class="md-nav__title" for="__nav_4">
          <span class="md-nav__icon md-icon"></span>
          用户指南
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../31-%E6%A6%82%E8%BF%B0/" class="md-nav__link">
        HttpRunner概念
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../32-%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/" class="md-nav__link">
        快速上手
      </a>
    </li>
  

            
          
            
              
  
  
    
  
  
    <li class="md-nav__item md-nav__item--active">
      
      <input class="md-nav__toggle md-toggle" data-md-toggle="toc" type="checkbox" id="__toc">
      
      
        
      
      
        <label class="md-nav__link md-nav__link--active" for="__toc">
          录制并生成测试用例
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <a href="./" class="md-nav__link md-nav__link--active">
        录制并生成测试用例
      </a>
      
        


<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#http" class="md-nav__link">
    捕获HTTP请求和响应
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#har" class="md-nav__link">
    将会话导出为HAR文件
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#har2case" class="md-nav__link">
    再使用har2case生成测试用例
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#pytest" class="md-nav__link">
    生成测试用例（pytest）
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#yamljson" class="md-nav__link">
    生成测试用例（YAML/JSON）
  </a>
  
</li>
      
    </ul>
  
</nav>
      
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../34-%E7%BC%96%E5%86%99%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B/" class="md-nav__link">
        编写测试用例
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../35-%E8%BF%90%E8%A1%8C%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B/" class="md-nav__link">
        运行测试用例
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../36-%E6%B5%8B%E8%AF%95%E6%8A%A5%E5%91%8A/" class="md-nav__link">
        测试报告
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../37-%E8%BF%90%E8%A1%8C%E8%B4%9F%E8%BD%BD%E6%B5%8B%E8%AF%95/" class="md-nav__link">
        运行负载测试
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../39-%E5%8F%91%E5%B8%83%E5%8E%86%E5%8F%B2/" class="md-nav__link">
        发布历史
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
        <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_5" type="checkbox" id="__nav_5" >
      
      
      
      
        <label class="md-nav__link" for="__nav_5">
          补充
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" aria-label="补充" data-md-level="1">
        <label class="md-nav__title" for="__nav_5">
          <span class="md-nav__icon md-icon"></span>
          补充
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/10-%E9%A1%B9%E7%9B%AE%E6%96%87%E4%BB%B6%E7%BB%84%E7%BB%87/" class="md-nav__link">
        项目文件组织
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/20-%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B%E7%BB%84%E7%BB%87/" class="md-nav__link">
        测试用例组织
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/30-Hook%E6%9C%BA%E5%88%B6/" class="md-nav__link">
        Hook机制
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/40-%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F/" class="md-nav__link">
        环境变量
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/50-%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B%E5%88%86%E5%B1%82/" class="md-nav__link">
        测试用例分层
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/60-%E5%8F%82%E6%95%B0%E5%8C%96%E6%95%B0%E6%8D%AE%E9%A9%B1%E5%8A%A8/" class="md-nav__link">
        参数化数据驱动
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/70-%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9C%BA%E6%99%AF/" class="md-nav__link">
        文件上传场景
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              
              <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    


<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#http" class="md-nav__link">
    捕获HTTP请求和响应
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#har" class="md-nav__link">
    将会话导出为HAR文件
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#har2case" class="md-nav__link">
    再使用har2case生成测试用例
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#pytest" class="md-nav__link">
    生成测试用例（pytest）
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#yamljson" class="md-nav__link">
    生成测试用例（YAML/JSON）
  </a>
  
</li>
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          <div class="md-content" data-md-component="content">
            <article class="md-content__inner md-typeset">
              
                
                
                <h1 id="_1">录制并生成测试用例</h1>
<h2 id="http">捕获HTTP请求和响应</h2>
<p>在编写测试用例之前，我们应该了解API的细节。使用Charles、Fiddler之类的web调试代理工具捕获HTTP流量是一个不错的选择。</p>
<p>例如，下图显示了用Charles工具将表单数据发布到postman-echo.com。</p>
<p><img alt="img" src="../img/charles-capture-example-postman-echo.png" /></p>
<h2 id="har">将会话导出为HAR文件</h2>
<p>然后，我们可以选择捕获的请求和响应，并将会话导出到HTTP存档（.har）文件。</p>
<p><img alt="img" src="https://docs.httprunner.org/images/charles-export-session.png" /></p>
<h2 id="har2case">再使用har2case生成测试用例</h2>
<p>当您获得HAR文件时，可以使用内置命令har2case将其转换为HttpRunner testcase。</p>
<p>命令帮助：</p>
<pre><code class="language-shell">$ har2case -h
usage: har2case har2case [-h] [-2y] [-2j] [--filter FILTER]
                         [--exclude EXCLUDE]
                         [har_source_file]

参数:
  har_source_file       指定HAR源文件
可选参数:
  -h, --help            show this help message and exit
  -2y, --to-yml, --to-yaml
                        Convert to YAML format, if not specified, convert to
                        pytest format by default.
  -2j, --to-json        Convert to JSON format, if not specified, convert to
                        pytest format by default.
  --filter FILTER       指定筛选器关键字，仅url包含筛选器字符串将被转换。
  --exclude EXCLUDE     指定排除关键字，包含排除字符串的url将被忽略，可以选择多个关键字与“|”连用
</code></pre>
<h2 id="pytest">生成测试用例（pytest）</h2>
<p>由于httprunner3.0.7，har2case默认情况下会将HAR文件转换为pytest，因此极为建议以pytest格式而不是以前的YAML/JSON格式编写和维护测试用例。</p>
<pre><code class="language-shell">$ har2case har/postman-echo-post-form.har
2020-06-15 15:08:01.187 | INFO     | httprunner.ext.har2case.core:gen_testcase:332 - Start to generate testcase from har/postman-echo-post-form.har
2020-06-15 15:08:01.187 | INFO     | httprunner.ext.har2case.core:_make_testcase:323 - Extract info from HAR file and prepare for testcase.
2020-06-15 15:08:01.191 | INFO     | httprunner.loader:load_dot_env_file:130 - Loading environment variables from /Users/debugtalk/Desktop/demo/.env
2020-06-15 15:08:01.191 | DEBUG    | httprunner.utils:set_os_environ:32 - Set OS environment variable: USERNAME
2020-06-15 15:08:01.191 | DEBUG    | httprunner.utils:set_os_environ:32 - Set OS environment variable: PASSWORD
2020-06-15 15:08:01.193 | INFO     | httprunner.make:make_testcase:310 - start to make testcase: /Users/debugtalk/Desktop/demo/har/postman-echo-post-form.har
2020-06-15 15:08:01.193 | INFO     | httprunner.make:make_testcase:383 - generated testcase: /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py
2020-06-15 15:08:01.194 | INFO     | httprunner.make:format_pytest_with_black:147 - format pytest cases with black ...
reformatted /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py
All done! ✨ 🍰 ✨
1 file reformatted.
2020-06-15 15:08:01.469 | INFO     | httprunner.ext.har2case.core:gen_testcase:353 - generated testcase: /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py
</code></pre>
<p>生成的pytest文件是一个标准Python文件，如下所示。</p>
<pre><code class="language-shell"># NOTE: Generated By HttpRunner v3.0.12
# FROM: har/postman-echo-post-form.har

from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

class TestCasePostmanEchoPostForm(HttpRunner):
    config = Config(&quot;testcase description&quot;).verify(False)

    teststeps = [
        Step(
            RunRequest(&quot;/get&quot;)
            .get(&quot;https://postman-echo.com/get&quot;)
            .with_params(**{&quot;foo1&quot;: &quot;bar1&quot;, &quot;foo2&quot;: &quot;bar2&quot;})
            .with_headers(
                **{
                    &quot;User-Agent&quot;: &quot;PostmanRuntime/7.24.1&quot;,
                    &quot;Accept&quot;: &quot;*/*&quot;,
                    &quot;Cache-Control&quot;: &quot;no-cache&quot;,
                    &quot;Postman-Token&quot;: &quot;6606343b-10e5-4165-a89f-6c301b762ce0&quot;,
                    &quot;Host&quot;: &quot;postman-echo.com&quot;,
                    &quot;Accept-Encoding&quot;: &quot;gzip, deflate, br&quot;,
                    &quot;Connection&quot;: &quot;keep-alive&quot;,
                    &quot;Cookie&quot;: &quot;sails.sid=s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU&quot;,
                }
            )
            .with_cookies(
                **{
                    &quot;sails.sid&quot;: &quot;s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU&quot;
                }
            )
            .validate()
            .assert_equal(&quot;status_code&quot;, 200)
            .assert_equal('headers.&quot;Content-Type&quot;', &quot;application/json; charset=utf-8&quot;)
            .assert_equal(
                &quot;body.url&quot;, &quot;https://postman-echo.com/get?foo1=bar1&amp;foo2=bar2&quot;
            )
        ),
        Step(
            RunRequest(&quot;/post&quot;)
            .post(&quot;https://postman-echo.com/post&quot;)
            .with_headers(
                **{
                    &quot;User-Agent&quot;: &quot;PostmanRuntime/7.24.1&quot;,
                    &quot;Accept&quot;: &quot;*/*&quot;,
                    &quot;Cache-Control&quot;: &quot;no-cache&quot;,
                    &quot;Postman-Token&quot;: &quot;3e408e9d-25ca-4b31-b04b-7f4898a8cd49&quot;,
                    &quot;Host&quot;: &quot;postman-echo.com&quot;,
                    &quot;Accept-Encoding&quot;: &quot;gzip, deflate, br&quot;,
                    &quot;Connection&quot;: &quot;keep-alive&quot;,
                    &quot;Content-Type&quot;: &quot;application/x-www-form-urlencoded&quot;,
                    &quot;Content-Length&quot;: &quot;19&quot;,
                    &quot;Cookie&quot;: &quot;sails.sid=s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU&quot;,
                }
            )
            .with_cookies(
                **{
                    &quot;sails.sid&quot;: &quot;s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU&quot;
                }
            )
            .with_data({&quot;foo1&quot;: &quot;bar1&quot;, &quot;foo2&quot;: &quot;bar2&quot;})
            .validate()
            .assert_equal(&quot;status_code&quot;, 200)
            .assert_equal('headers.&quot;Content-Type&quot;', &quot;application/json; charset=utf-8&quot;)
            .assert_equal(&quot;body.data&quot;, &quot;&quot;)
            .assert_equal(&quot;body.url&quot;, &quot;https://postman-echo.com/post&quot;)
        ),
    ]


if __name__ == &quot;__main__&quot;:
    TestCasePostmanEchoPostForm().test_start()
</code></pre>
<p>它可以使用hrun命令或原生pytest命令运行。事实上，hrun只是pytest的包装，因此效果基本相同。</p>
<pre><code class="language-shell">$ hrun har/postman_echo_post_form_test.py  
2020-06-15 15:23:03.502 | INFO     | httprunner.loader:load_dot_env_file:130 - Loading environment variables from /Users/debugtalk/Desktop/demo/.env
2020-06-15 15:23:03.502 | DEBUG    | httprunner.utils:set_os_environ:32 - Set OS environment variable: USERNAME
2020-06-15 15:23:03.502 | DEBUG    | httprunner.utils:set_os_environ:32 - Set OS environment variable: PASSWORD
2020-06-15 15:23:03.503 | INFO     | httprunner.make:format_pytest_with_black:147 - format pytest cases with black ...
All done! ✨ 🍰 ✨
1 file left unchanged.
2020-06-15 15:23:03.662 | INFO     | httprunner.cli:main_run:56 - start to run tests with pytest. HttpRunner version: 3.0.12
====================================================================== test session starts ======================================================================
platform darwin -- Python 3.7.5, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /Users/debugtalk/Desktop/demo
plugins: metadata-1.9.0, allure-pytest-2.8.16, html-2.1.1
collected 1 item                                                          
har/postman_echo_post_form_test.py .                                                                                                                      [100%]

======================================================================= 1 passed in 2.60s =======================================================================
</code></pre>
<pre><code class="language-shell">$ pytest har/postman_echo_post_form_test.py 
====================================================================== test session starts ======================================================================
platform darwin -- Python 3.7.5, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /Users/debugtalk/Desktop/demo
plugins: metadata-1.9.0, allure-pytest-2.8.16, html-2.1.1
collected 1 item   
har/postman_echo_post_form_test.py .                                                                                                                      [100%]

================================================================= 1 passed, 1 warning in 4.11s ==================================================================
</code></pre>
<h2 id="yamljson">生成测试用例（YAML/JSON）</h2>
<p>当然，您也可以生成以前的YAML/JSON测试用例格式。只需要在上述命令中添加-2y或者-2j参数。</p>
<pre><code class="language-shell">$ har2case har/postman-echo-post-form.har -2j
2020-06-15 15:32:02.955 | INFO     | httprunner.ext.har2case.core:gen_testcase:332 - Start to generate testcase from har/postman-echo-post-form.har
2020-06-15 15:32:02.955 | INFO     | httprunner.ext.har2case.core:_make_testcase:323 - Extract info from HAR file and prepare for testcase.
2020-06-15 15:32:02.958 | INFO     | httprunner.ext.har2case.utils:dump_json:122 - dump testcase to JSON format.
2020-06-15 15:32:02.959 | INFO     | httprunner.ext.har2case.utils:dump_json:131 - Generate JSON testcase successfully: har/postman-echo-post-form.json
2020-06-15 15:32:02.959 | INFO     | httprunner.ext.har2case.core:gen_testcase:353 - generated testcase: har/postman-echo-post-form.json
</code></pre>
<p>生成的JSON文件内容如下：</p>
<pre><code class="language-json">{
    &quot;config&quot;: {
        &quot;name&quot;: &quot;testcase description&quot;,
        &quot;variables&quot;: {},
        &quot;verify&quot;: false
    },
    &quot;teststeps&quot;: [
        {
            &quot;name&quot;: &quot;/get&quot;,
            &quot;request&quot;: {
                &quot;url&quot;: &quot;https://postman-echo.com/get&quot;,
                &quot;params&quot;: {
                    &quot;foo1&quot;: &quot;bar1&quot;,
                    &quot;foo2&quot;: &quot;bar2&quot;
                },
                &quot;method&quot;: &quot;GET&quot;,
                &quot;cookies&quot;: {
                    &quot;sails.sid&quot;: &quot;s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU&quot;
                },
                &quot;headers&quot;: {
                    &quot;User-Agent&quot;: &quot;PostmanRuntime/7.24.1&quot;,
                    &quot;Accept&quot;: &quot;*/*&quot;,
                    &quot;Cache-Control&quot;: &quot;no-cache&quot;,
                    &quot;Postman-Token&quot;: &quot;6606343b-10e5-4165-a89f-6c301b762ce0&quot;,
                    &quot;Host&quot;: &quot;postman-echo.com&quot;,
                    &quot;Accept-Encoding&quot;: &quot;gzip, deflate, br&quot;,
                    &quot;Connection&quot;: &quot;keep-alive&quot;,
                    &quot;Cookie&quot;: &quot;sails.sid=s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU&quot;
                }
            },
            &quot;validate&quot;: [
                {
                    &quot;eq&quot;: [
                        &quot;status_code&quot;,
                        200
                    ]
                },
                {
                    &quot;eq&quot;: [
                        &quot;headers.Content-Type&quot;,
                        &quot;application/json; charset=utf-8&quot;
                    ]
                },
                {
                    &quot;eq&quot;: [
                        &quot;body.url&quot;,
                        &quot;https://postman-echo.com/get?foo1=bar1&amp;foo2=bar2&quot;
                    ]
                }
            ]
        },
        {
            &quot;name&quot;: &quot;/post&quot;,
            &quot;request&quot;: {
                &quot;url&quot;: &quot;https://postman-echo.com/post&quot;,
                &quot;method&quot;: &quot;POST&quot;,
                &quot;cookies&quot;: {
                    &quot;sails.sid&quot;: &quot;s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU&quot;
                },
                &quot;headers&quot;: {
                    &quot;User-Agent&quot;: &quot;PostmanRuntime/7.24.1&quot;,
                    &quot;Accept&quot;: &quot;*/*&quot;,
                    &quot;Cache-Control&quot;: &quot;no-cache&quot;,
                    &quot;Postman-Token&quot;: &quot;3e408e9d-25ca-4b31-b04b-7f4898a8cd49&quot;,
                    &quot;Host&quot;: &quot;postman-echo.com&quot;,
                    &quot;Accept-Encoding&quot;: &quot;gzip, deflate, br&quot;,
                    &quot;Connection&quot;: &quot;keep-alive&quot;,
                    &quot;Content-Type&quot;: &quot;application/x-www-form-urlencoded&quot;,
                    &quot;Content-Length&quot;: &quot;19&quot;,
                    &quot;Cookie&quot;: &quot;sails.sid=s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU&quot;
                },
                &quot;data&quot;: {
                    &quot;foo1&quot;: &quot;bar1&quot;,
                    &quot;foo2&quot;: &quot;bar2&quot;
                }
            },
            &quot;validate&quot;: [
                {
                    &quot;eq&quot;: [
                        &quot;status_code&quot;,
                        200
                    ]
                },
                {
                    &quot;eq&quot;: [
                        &quot;headers.Content-Type&quot;,
                        &quot;application/json; charset=utf-8&quot;
                    ]
                },
                {
                    &quot;eq&quot;: [
                        &quot;body.data&quot;,
                        &quot;&quot;
                    ]
                },
                {
                    &quot;eq&quot;: [
                        &quot;body.url&quot;,
                        &quot;https://postman-echo.com/post&quot;
                    ]
                }
            ]
        }
    ]
}
</code></pre>
<p>YAML/JSON测试用例与pytest测试用例具有相同的信息，您可以使用hrun命令运行YAML/JSON测试用例。</p>
<pre><code class="language-shell">$ hrun har/postman-echo-post-form.json 
2020-06-15 15:37:15.621 | INFO     | httprunner.loader:load_dot_env_file:130 - Loading environment variables from /Users/debugtalk/Desktop/demo/.env
2020-06-15 15:37:15.622 | DEBUG    | httprunner.utils:set_os_environ:32 - Set OS environment variable: USERNAME
2020-06-15 15:37:15.622 | DEBUG    | httprunner.utils:set_os_environ:32 - Set OS environment variable: PASSWORD
2020-06-15 15:37:15.623 | INFO     | httprunner.make:make_testcase:310 - start to make testcase: /Users/debugtalk/Desktop/demo/har/postman-echo-post-form.json
2020-06-15 15:37:15.625 | INFO     | httprunner.make:make_testcase:383 - generated testcase: /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py
2020-06-15 15:37:15.625 | INFO     | httprunner.make:format_pytest_with_black:147 - format pytest cases with black ...
reformatted /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py
All done! ✨ 🍰 ✨
1 file reformatted, 1 file left unchanged.
2020-06-15 15:37:15.962 | INFO     | httprunner.cli:main_run:56 - start to run tests with pytest. HttpRunner version: 3.0.12
====================================================================== test session starts ======================================================================
platform darwin -- Python 3.7.5, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /Users/debugtalk/Desktop/demo
plugins: metadata-1.9.0, allure-pytest-2.8.16, html-2.1.1
collected 1 item                                                                                                                                                

har/postman_echo_post_form_test.py .                                                                                                                      [100%]

======================================================================= 1 passed in 2.03s =======================================================================
</code></pre>
                
              
              
                


              
            </article>
          </div>
        </div>
        
      </main>
      
        
<footer class="md-footer">
  
    <nav class="md-footer__inner md-grid" aria-label="Footer">
      
        
        <a href="../32-%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 快速上手" rel="prev">
          <div class="md-footer__button md-icon">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12z"/></svg>
          </div>
          <div class="md-footer__title">
            <div class="md-ellipsis">
              <span class="md-footer__direction">
                上一页
              </span>
              快速上手
            </div>
          </div>
        </a>
      
      
        
        <a href="../34-%E7%BC%96%E5%86%99%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B/" class="md-footer__link md-footer__link--next" aria-label="下一页: 编写测试用例" rel="next">
          <div class="md-footer__title">
            <div class="md-ellipsis">
              <span class="md-footer__direction">
                下一页
              </span>
              编写测试用例
            </div>
          </div>
          <div class="md-footer__button md-icon">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4z"/></svg>
          </div>
        </a>
      
    </nav>
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-footer-copyright">
        
        
          Made with
          <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
            Material for MkDocs
          </a>
        
        
      </div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    <script id="__config" type="application/json">{"base": "../..", "features": [], "translations": {"clipboard.copy": "\u590d\u5236", "clipboard.copied": "\u5df2\u590d\u5236", "search.config.lang": "ja", "search.config.pipeline": "trimmer, stemmer", "search.config.separator": "[\\uff0c\\u3002]+", "search.placeholder": "\u641c\u7d22", "search.result.placeholder": "\u952e\u5165\u4ee5\u5f00\u59cb\u641c\u7d22", "search.result.none": "\u6ca1\u6709\u627e\u5230\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.one": "\u627e\u5230 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.other": "# \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.term.missing": "Missing", "select.version.title": "Select version"}, "search": "../../assets/javascripts/workers/search.fcfe8b6d.min.js", "version": null}</script>
    
    
      <script src="../../assets/javascripts/bundle.b1047164.min.js"></script>
      
    
  </body>
</html>